<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" class="mr10" @click="add" size="small"
          >添加会员等级</el-button
        >
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
      >
        <el-table-column prop="id" label="ID" min-width="50" />
        <el-table-column prop="levelName" label="等级名称" min-width="150" />
        <el-table-column prop="levelSort" label="等级级别" min-width="150" />
        <el-table-column prop="goodName" label="商品名称" min-width="150" />
        <el-table-column prop="reward" label="推荐奖励" min-width="100" />
        <el-table-column
          label="操作"
          min-width="120"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="edit(scope.row.id)"
              class="mr10"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.id, scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <creat-grade ref="grades"></creat-grade>
  </div>
</template>

<script>
import { levelGoodListApi, levelGoodDeleteApi } from "@/api/user";
import creatGrade from "./creatGrade";
export default {
  name: "Grade",
  components: { creatGrade },
  data() {
    return {
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        isDel: 0
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    seachList() {
      this.tableFrom.page = 1;
      this.getList();
    },
    add() {
      this.$refs.grades.dialogVisible = true;
      this.$refs.grades.isLockStore = false;
      this.$refs.grades.info(null);
    },
    edit(id) {
      this.$refs.grades.dialogVisible = true;
      this.$refs.grades.info(id);
    },
    // 列表
    getList() {
      this.listLoading = true;
      levelGoodListApi(this.tableFrom)
        .then(res => {
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    // 删除
    handleDelete(id) {
      this.$modalSure("删除吗？所有用户已经关联的数据都会清除").then(() => {
        levelGoodDeleteApi({ id: id }).then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    }
  }
};
</script>

<style scoped lang="scss"></style>
